package project.controllers;

import Utils.web.WebUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author d11yu
 */
@Controller
public class FileUploadDownLoadController {
    @RequestMapping("/testDownload")
    public ResponseEntity<byte[]> testDownload(HttpSession session) throws IOException {
        /*ServletContext servletContext = session.getServletContext();
        String realPath = servletContext.getRealPath("/static/img/25岁男人的存款，现实与理想的差距.jpg");
        FileInputStream is = new FileInputStream(realPath);
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename=25岁男人的存款，现实与理想的差距.jpg");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);
        is.close();*/

        return WebUtils.download(session, "/static/img/25岁男人的存款，现实与理想的差距.jpg");
    }

    @RequestMapping("/testUpload")
    public String testUpload(MultipartFile photo, HttpSession session) throws IOException {
        /*String fileName = photo.getOriginalFilename();
        ServletContext servletContext = session.getServletContext();
        String photoPath = servletContext.getRealPath("photo");
        FsUtils.createDir(photoPath);
        String finalPath = photoPath + File.separator + fileName;
        photo.transferTo(new File(finalPath));*/
//        WebUtils.upload(session, photo, "photos");
        WebUtils.uploadUUID(session, photo, "books");
        return "success";
    }
}
